import 'dart:io';

import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:tvlives/model/category.dart';
import 'package:tvlives/provider/provider.dart';
import 'package:tvlives/route/Application.dart';
import 'package:tvlives/route/Routes.dart';
import 'package:tvlives/utils/StorageUtil.dart';
import 'package:tvlives/values/Storages.dart';

class Global{

   static bool isFirstOpen = false;
   static CategoryList categorys = new CategoryList();
   static ThemeInfo themeInfo = new ThemeInfo();
   static Future init() async{
      WidgetsFlutterBinding.ensureInitialized();

      await StorageUtil.init();

      Router router = Router();
      Routes.configureRoutes(router);
      Application.router = router;
      
      isFirstOpen = !StorageUtil().getBool(STORAGE_DEVICE_ALREADY_OPEN_KEY);
      if(isFirstOpen){
        StorageUtil().setBool(STORAGE_DEVICE_ALREADY_OPEN_KEY, true);
      }

      String themeColor = StorageUtil().getString(THEME_INFO);
      themeColor == null ? themeInfo.setTheme("blue"):themeInfo.setTheme(themeColor);

      //读取分类信息 没有的话给个默认值

      List _categoriesJSON = StorageUtil().getJSON(RECOMMENDATION_CATEGORY_LIST);

      if (_categoriesJSON != null) {
         //临时办法
         List<Category> clist = [];
         _categoriesJSON.forEach((f) {
            clist.add(Category.fromJson(f));
         });
         categorys.setCategoryList(clist);
      } else {
         //设置默认分类 倒时候换成api 读取
         Category c1 = new Category(cid: '0', name: "推荐");
         Category c2 = new Category(cid: '181', name: "王者荣耀");
         Category c3 = new Category(cid: '1', name: "英雄联盟");
         Category c4 = new Category(cid: '270', name: "绝地求生");
         Category c5 = new Category(cid: '311', name: "颜值");
         Category c6 = new Category(cid: '1008', name: "舞蹈");
         List<Category> list = [];
         list.add(c1);
         list.add(c2);
         list.add(c3);
         list.add(c4);
         list.add(c5);
         list.add(c6);
         categorys.setCategoryList(list);
         //保存localstorage
         StorageUtil().setJSON(RECOMMENDATION_CATEGORY_LIST, list);
      }

      // android 状态栏为透明的沉浸
      if(Platform.isAndroid){
         SystemUiOverlayStyle systemUiOverlayStyle =
              SystemUiOverlayStyle(statusBarColor: Colors.transparent);
        SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
      }

   }
}